package net.betou.cms.action.admin;

import java.awt.Color;
import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import net.betou.cms.entity.main.MarkConfig;
import net.betou.cms.manager.assist.CmsFileMng;
import net.betou.common.image.ImageScale;
import net.betou.common.image.ImageUtils;
import net.betou.common.upload.FileRepository;
import net.betou.common.web.springmvc.RealPathResolver;
import net.betou.core.manager.DbFileMng;
import net.betou.core.web.WebErrors;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 忘记一切、重新开始
 */
public class AbstractUpload{
	protected WebErrors validate( String filename, MultipartFile file, HttpServletRequest request ) {
		WebErrors errors = WebErrors.create( request );
		if( file == null ){
			errors.addErrorCode( "imageupload.error.noFileToUpload" );
			return errors;
		}
		if( StringUtils.isBlank( filename ) ){
			filename = file.getOriginalFilename( );
		}
		String ext = FilenameUtils.getExtension( filename );
		if( !ImageUtils.isValidImageExt( ext ) ){
			errors.addErrorCode( "imageupload.error.notSupportExt", ext );
			return errors;
		}
		try{
			if( !ImageUtils.isImage( file.getInputStream( ) ) ){
				errors.addErrorCode( "imageupload.error.notImage", ext );
				return errors;
			}
		}catch( IOException e ){
			errors.addErrorCode( "imageupload.error.ioError", ext );
			return errors;
		}
		return errors;
	}

	protected File mark( MultipartFile file, MarkConfig conf ) throws Exception {
		String path = System.getProperty( "java.io.tmpdir" );
		File tempFile = new File( path, String.valueOf( System.currentTimeMillis( ) ) );
		file.transferTo( tempFile );
		boolean imgMark = !StringUtils.isBlank( conf.getImagePath( ) );
		if( imgMark ){
			File markImg = new File( realPathResolver.get( conf.getImagePath( ) ) );
			imageScale.imageMark( tempFile, tempFile, conf.getMinWidth( ), conf.getMinHeight( ), conf.getPos( ), conf.getOffsetX( ), conf.getOffsetY( ), markImg );
		}else{
			imageScale.imageMark( tempFile, tempFile, conf.getMinWidth( ), conf.getMinHeight( ), conf.getPos( ), conf.getOffsetX( ), conf.getOffsetY( ), conf.getContent( ), Color.decode( conf.getColor( ) ), conf.getSize( ), conf.getAlpha( ) );
		}
		return tempFile;
	}

	@Autowired
	protected FileRepository fileRepository;
	@Autowired
	protected DbFileMng dbFileMng;
	@Autowired
	protected ImageScale imageScale;
	@Autowired
	protected RealPathResolver realPathResolver;
	@Autowired
	protected CmsFileMng fileMng;
}
